--[[
    交易行数据管理类 
--]]

require "Common/define"
require "Logic/Config"
require "Common/functions"
require "Common/Class"
require "Logic/BrokerUtils"

BrokerManager = BrokerManager or {}
local this = BrokerManager

--上架出售限制
this.IsSellSelected = false
this.SellMinValue = 1
this.SellMaxValue = 99
--上架进化限制
this.IsEvolutionSelected = false
this.EvolutionMinValue = 1
this.EvolutionMaxValue = 99
this.PetAddedFee = 0.1                  --上架税费
this.PetConsignmentDuration = 99        --飞飞交易行飞飞上架持续时间（秒）
this.PetConsignmentMaxCount = 1         --飞飞交易行最多同时上架飞飞数量

this.BrokerSellPetList = {}         --售卖的宠物列表
this.BrokerSellPetPage = 1          --当前刷新到最大页数
this.HasBrokerSellNextPage = false

this.BrokerEvolutionPetList = {}    --进化的宠物列表
this.BrokerEvolutionPetPage = 1     --当前刷新到的最大页数
this.HasBrokerEvolutionNextPage = false

--订单列表
this.MyConsignmentList = {}         --我的寄售宠物列表

--交易行的宠物细节界面
this.BrokerPetType = 1          --BrokerUtils.BrokerPetType 枚举类型
this.DetailSelectedPetId = 0    --选中需要查看细节的宠物id

--上架
this.BrokerAddedPetId = 0       --上架宠物id
this.clickObj = {}
this.isCanClick = true
this.canShopPet = true

this.BrokerSellPetPriceRangeList = {}          -- 售卖的宠物价格筛选列表
this.BrokerEvolutionPetPriceRangeList = {}     -- 进化的宠物价格筛选列表

function BrokerManager.Init()
    print("BrokerManager Init")
    local _sellConfigList = HelperFunc.GetGlobalVlue(503)
    this.SellMinValue = _sellConfigList[2]
    this.SellMaxValue = _sellConfigList[3]

    local _evolutionConfigList = HelperFunc.GetGlobalVlue(504)
    this.EvolutionMinValue = _evolutionConfigList[2]
    this.EvolutionMaxValue = _evolutionConfigList[3]

    this.PetAddedFee = HelperFunc.GetGlobalNumVlue(505)
    this.PetConsignmentDuration = HelperFunc.GetGlobalNumVlue(507)
    this.PetConsignmentMaxCount = HelperFunc.GetGlobalNumVlue(508)
    BrokerHandler.ReqBrokerGoodsList(BrokerUtils.BrokerPetType.Myself,1)
end
--获取当前飞飞币数量
function this.GetCurrentPetCoin()
    return ItemManager.getBagNumByTableID(CurrencyEnum.PetCoin) or 0
end
--检测是否超过宠物背包上限
function this.CheckPetBagMaxCount()
    return #(PetManager.PetInfoList) >= HelperFunc.GetGlobalNumVlue(119)
end
--检测是否为当前角色player
function this.CheckIsMyPlayer(in_playerId)
    local _myPlayerId = G.serverData["roleInfo"].show.playerId
    return tostring(in_playerId) == tostring(_myPlayerId)
end
--宠物交易行每页显示个数
function this.GetPerPageMaxItem()
    return HelperFunc.GetGlobalNumVlue(502)
end
--检测宠物是否已经上架
function this.CheckPetCanAdded(in_petId)
    for _, petInfo in pairs(this.MyConsignmentList) do
        if tostring(petInfo.petId) == tostring(in_petId) then
            return true
        end
    end
    return false
end

-- <bean name="BrokerGoodsBean" explain="寄售商品信息">
-- <field class="id" name="id" explain="寄售商品唯一ID"/>
-- <field class="id" name="type" explain="类型：1宠物"/>
-- <field class="int" name="playerId" explain="玩家唯一ID"/>
-- <field class="int" name="playerName" explain="玩家名称"/>
-- <field class="int" name="playerPic" explain="玩家头像"/>
-- <field class="int" name="playerPicFrame" explain="玩家头像框"/>
-- <field class="int" name="sellPrice" explain="出售价格"/>
-- <field class="int" name="evolutionPrice" explain="进化价格"/>
-- <field class="PetInfo" name="petInfo" explain="宠物信息"/>
-- </bean>
function this.AddBrokerPet(in_brokerGoodsBean,in_brokerType)
    local _newPetInfo = PetManager.GetNewPetInfo(in_brokerGoodsBean.petInfo,in_brokerGoodsBean.playerName)
    _newPetInfo.goodsId = tostring(in_brokerGoodsBean.id)
    _newPetInfo.playerId = tostring(in_brokerGoodsBean.playerId)
    _newPetInfo.playerName = tostring(in_brokerGoodsBean.playerName)
    _newPetInfo.playerPic = tostring(in_brokerGoodsBean.playerPic)
    _newPetInfo.playerPicFrame = in_brokerGoodsBean.playerPicFrame
    _newPetInfo.sellPrice = tonumber(in_brokerGoodsBean.sellPrice)
    _newPetInfo.evolutionPrice = tonumber(in_brokerGoodsBean.evolutionPrice)
    _newPetInfo.registerTime = tonumber(tostring(in_brokerGoodsBean.registerTime))/ 1000
    
    if in_brokerType== BrokerUtils.BrokerPetType.Myself then
        table.insert(this.MyConsignmentList,_newPetInfo)
    elseif in_brokerType== BrokerUtils.BrokerPetType.Buy then
        table.insert(this.BrokerSellPetList, _newPetInfo)
    elseif in_brokerType== BrokerUtils.BrokerPetType.Evolution then
        table.insert(this.BrokerEvolutionPetList, _newPetInfo)
    end
end
--移除列表中的元素
function this.RemovePetByTypeAndIndex(type,index)
    if type== BrokerUtils.BrokerPetType.Myself then
        this.MyConsignmentList[index] = nil
    elseif type== BrokerUtils.BrokerPetType.Buy then
        this.BrokerSellPetList[index] = nil
    elseif type== BrokerUtils.BrokerPetType.Evolution then
        this.BrokerEvolutionPetList[index] = nil
    end
end

function this.SortListByBrokerType(in_brokerType)
    if in_brokerType== BrokerUtils.BrokerPetType.Myself then
        --table.sort(this.MyConsignmentList,this.sortByRegisterTime)
    elseif in_brokerType== BrokerUtils.BrokerPetType.Buy then
        --table.sort(this.BrokerSellPetList, this.sortByRegisterTime)
    elseif in_brokerType== BrokerUtils.BrokerPetType.Evolution then
        --table.sort(this.BrokerEvolutionPetList, this.sortByRegisterTime)
    end
end
function this.sortByRegisterTime(in_petInfo1,in_petInfo2)
    return in_petInfo1.registerTime > in_petInfo2.registerTime
end

function this.GetPetInfoByPetId(in_petId,in_brokerPetType)
    in_petId = tostring(in_petId)
    if in_brokerPetType == BrokerUtils.BrokerPetType.Buy then
        for _, petInfo in pairs(this.BrokerSellPetList) do
            if tostring(petInfo.petId) == in_petId then
                return petInfo
            end
        end
    elseif in_brokerPetType == BrokerUtils.BrokerPetType.Evolution then
        for _, petInfo in pairs(this.BrokerEvolutionPetList) do
            if tostring(petInfo.petId) == in_petId then
                return petInfo
            end
        end
    elseif in_brokerPetType == BrokerUtils.BrokerPetType.Myself then
        for _, petInfo in pairs(this.MyConsignmentList) do
            if tostring(petInfo.petId) == in_petId then
                return petInfo
            end
        end
    end
    
    return nil
end

function this.GetPetInfoByIndex(in_index,in_brokerPetType)
    local _petInfo
    if in_brokerPetType == BrokerUtils.BrokerPetType.Buy then
        _petInfo = BrokerManager.BrokerSellPetList[in_index]
    elseif in_brokerPetType == BrokerUtils.BrokerPetType.Evolution then
        _petInfo = BrokerManager.BrokerEvolutionPetList[in_index]
    elseif in_brokerPetType == BrokerUtils.BrokerPetType.Myself then
        _petInfo = BrokerManager.MyConsignmentList[in_index]
    end
    return _petInfo
end

--获取当前可上架的宠物列表
function this.GetCanAddedPetList()
    local _canAddedList = {}
    local _isInConsignment = false
    for _, petInfo in pairs(PetManager.PetInfoList) do
        _isInConsignment = false
        for _,myConsignmentPetInfo  in pairs(this.MyConsignmentList) do
            if myConsignmentPetInfo.petId == petInfo.petId then
                _isInConsignment = true
                break
            end
        end
        if not _isInConsignment then
            table.insert(_canAddedList, petInfo)
        end
    end
    return _canAddedList
end

--- 根据价格筛选对应宠物列表
--- @param type number 列表类型
--- @param min number 最低价格
--- @param max number 最高价格
function this.ParsePetPriceRangeList(type, min, max)
    min = min or 0
    max = max or Mathf.Infinity
    if type == BrokerUtils.BrokerPetType.Buy then
        this.BrokerSellPetPriceRangeList = {}
        for _, petInfo in pairs(BrokerManager.BrokerSellPetList) do
            local price = tonumber(petInfo.sellPrice)
            if price >= min and price <= max then
                table.insert(this.BrokerSellPetPriceRangeList, petInfo)
            end
        end
    elseif type == BrokerUtils.BrokerPetType.Evolution then
        this.BrokerEvolutionPetPriceRangeList = {}
        for _, petInfo in pairs(BrokerManager.BrokerEvolutionPetList) do
            local price = tonumber(petInfo.evolutionPrice)
            if price >= min and price <= max then
                table.insert(this.BrokerEvolutionPetPriceRangeList, petInfo)
            end
        end
    end
end

--- 根据索引从价格筛选列表中读取宠物信息
--- @param index number 索引
--- @param type number 列表类型
--- @return any 宠物信息
function this.GetPetInfoFromPriceRangeByIndex(index, type)
    if type == BrokerUtils.BrokerPetType.Buy then
        return BrokerManager.BrokerSellPetPriceRangeList[index]
    elseif type == BrokerUtils.BrokerPetType.Evolution then
        return BrokerManager.BrokerEvolutionPetPriceRangeList[index]
    end
    return nil
end

return BrokerManager